package com.jt.controller;

import com.jt.pojo.User;
import org.springframework.web.bind.annotation.*;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin  //主要解决跨域的问题
@RequestMapping("/axios")
public class AxiosController {
    /**
     * 1.业务需求说明，根据id查询用户的信息
     * url地址：
     *      http://localhost:8080/axios/getUserById?id=100
     * 参数信息：    id=100
     * 返回值：User对象的JSON  伪造一个User对象
     */



    @RequestMapping("/getUserById")
    public User getUserById(Integer id){
        User user = new User();
        user.setName("唐天舞");
        user.setAge(18);
        user.setSex("秀吉");
        user.setId(id);
        String[] arr={"修炼","觉醒武魂","魂师","大魂师"};
        user.setHobby(arr);
        return user;
    }

    /**
     * url地址：http://localhost:8080/axios/getUserByNA?id=1&name=唐天舞
     */
    @RequestMapping("/getUserByNA")
    public List<User> getUserByNA(User user){
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user);
        return list;
    }

    /**
     * Url地址：http://localhost:8080/axios/findUserByNS/唐天舞/女
     * 参数：name/sex
     * 返回值：List<User>
     */
    @RequestMapping("/findUserByNS/{name}/{sex}")
    public List<User> findUserByNS(User user){
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setName("李太玄");
        user1.setAge(18);
        user1.setSex("秀吉");
        user1.setId(1);
        String[] arr={"都市","修仙","兵王","转世重生"};
        user1.setHobby(arr);
        User user2 = new User();
        user2.setName("唐天舞");
        user2.setAge(18);
        user2.setSex("女");
        user2.setId(1);
        String[] arr1={"修炼","觉醒武魂","魂师","大魂师"};
        user2.setHobby(arr1);
        list.add(user1);
        list.add(user2);
        List<User> list1 = new ArrayList<>();
        for (int i = 0 ; i < list.size();i++){
            if (((list.get(i).getName()).equals(user.getName()))&&((list.get(i).getSex()).equals(user.getSex()))){
                list1.add(list.get(i));
                System.out.println("查到了");
            }else{
                System.out.println("没查到");
            }
        }
        return list1;
    }

    /**
     * axios.post
     * urll路径：http://localhost:8080/axios/saveUser
     * 参数：{id:100,name:"唐天舞",age:18,sex:"女"}
     * 返回值：新增用户成功
     * 难点：
     *      1.GET请求数据是通过？key=value&key2=value2的方式获取
     *      2.POST请求 数据是json串 数据结构不同，所以不能使用User对象接受
     *      3.JSON串 想办法转换为User对象
     *        User转换为JSON串 @ResponseBody
     *        JSON串转换为User @RequestBody
     *      4.JSON串的转换 要求json串中的属性与对象中的属性一致
     *                   并且我要赋值时调用对象的set方法
     *      5.改进:只能接受固定类型的亲求
     *             @PostMapping("/saveUser")
     *             @GetMapping
     *             @PutMapping
     *             @DeleteMapping
     */
//    @RequestMapping(value = "/saveUser" , method = RequestMethod.POST)
    @PostMapping("/saveUser")
    public User saveUser(@RequestBody User user){
        String[] arr={"修炼","觉醒武魂","魂师","大魂师"};
        user.setHobby(arr);
        return user;
    }

    /**
     * 业务需求
     * 		完成用户的修改操作.将id=2的数据进行修改name改为李太玄 sex改为男
     * url:http://localhost:8080/axios/updateUser
     * 返回值: 修改成功!!!  数据要求后端打印
     */
    @PutMapping("/updateUser")
    public List<User> updateUser(@RequestBody User user){
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setName("李七夜");
        user1.setAge(18);
        user1.setSex("秀吉");
        user1.setId(2);
        String[] arr={"都市","修仙","兵王","转世重生"};
        user1.setHobby(arr);
        User user2 = new User();
        user2.setName("唐天舞");
        user2.setAge(18);
        user2.setSex("女");
        user2.setId(1);
        String[] arr1={"修炼","觉醒武魂","魂师","大魂师"};
        user2.setHobby(arr1);
        list.add(user1);
        list.add(user2);
        List<User> list1 = new ArrayList<>();
        for (int i = 0 ; i < list.size();i++){
            if (list.get(i).getId()==user.getId()){
                list.get(i).setName(user.getName());
                list.get(i).setSex(user.getSex());
                list1.add(list.get(i));
                System.out.println("修改成功");
                break;
            }
        }
        return list1;
    }
    /**
     * 业务需求
     * 		根据id删除数据
     * url:http://localhost:8080/axios/deleteUserById?id=1
     * url:http://localhost:8080/axios/deleteUserById/1
     * url:http://localhost:8080/axios/deleteUserById
     * 返回值: 删除成功!!!  数据要求后端打印
     */
    @DeleteMapping("/deleteUserById/{id}")
    public List<User> deleteUserById(@PathVariable Integer id){
        List<User> list = new ArrayList<>();
        User user1 = new User();
        user1.setName("李七夜");
        user1.setAge(18);
        user1.setSex("秀吉");
        user1.setId(2);
        String[] arr={"都市","修仙","兵王","转世重生"};
        user1.setHobby(arr);
        User user2 = new User();
        user2.setName("唐天舞");
        user2.setAge(18);
        user2.setSex("女");
        user2.setId(1);
        String[] arr1={"修炼","觉醒武魂","魂师","大魂师"};
        user2.setHobby(arr1);
        list.add(user1);
        list.add(user2);
        for (int i = 0 ; i < list.size();i++){
            if (list.get(i).getId().equals(id)){
                list.remove(i);
                System.out.println("删除成功");
                break;
            }
        }
        return list;
    }

}
